<?php

use App\Models\CollectionModel;
use App\Libs\uploadImg;
use Illuminate\Support\Facades\Storage;

/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2019/12/17  14:24
 */

//获取分类 一维数组
function getTree($arr,$pid=0,$level=0)
{
    static $list = [];
    foreach ($arr as $key => $value) {
        if ($value["parent_id"] == $pid) {
            $value["level"] = $level;

            $flg = str_repeat('|--',$level);
            // 更新 名称值
            $value['text'] = $flg.$value['text'];

            $list[] = $value;
            unset($arr[$key]); //删除已经排好的数据为了减少遍历的次数，当然递归本身就很费神就是了
            getTree($arr,$value["id"],$level+1);
        }
    }
    return $list;
}

function returnMsg($code,$msg,$data=null){
    return response() -> json([
        'error_code' => $code,
        'msg' => $msg,
        'data' =>$data
    ]);
}

// 获取当前域名
function getDomain(){
    return $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'];
}

function httpCurl($url = '',$type = 'get',$data = [],$header = []){

    // 初始化
    $ch = curl_init();

    // 设置curl参数
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); // 绕过ssl
    curl_setopt($ch,CURLOPT_HTTPHEADER, $header); // 添加header信息

    // post提交需要提交参数
    if($type == 'post'){
        curl_setopt($ch,CURLOPT_POST,1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    }

    // 采集
    $result = curl_exec($ch);

    // 关闭
    curl_close($ch);
    return $result;
}

/**
 * 从网上下载图片保存到服务器
 * @param $url 图片网址
 * @param $path 保存到服务器的路径
 */
function downloadImages($url,$path) {
    $ch = curl_init ($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); // 绕过ssl
    $img = curl_exec($ch);

    curl_close($ch);
    // $filename = pathinfo($url, PATHINFO_BASENAME); // 获取文件名
    //$image_name就是要保存到什么路径,默认只写文件名的话保存到根目录

    //保存的文件名称用的是链接里面的名称
    $fp = fopen($path,'w');
    fwrite($fp, $img);
    fclose($fp);
}


function getfirstchar($s0) {
    $fchar = ord(substr($s0, 0, 1));
    if (($fchar >= ord("a") and $fchar <= ord("z"))or($fchar >= ord("A") and $fchar <= ord("Z"))) return strtoupper(chr($fchar));
    $s = iconv("UTF-8", "GBK", $s0);
    $asc = ord($s{0}) * 256 + ord($s{1})-65536;
    if ($asc >= -20319 and $asc <= -20284)return "A";
    if ($asc >= -20283 and $asc <= -19776)return "B";
    if ($asc >= -19775 and $asc <= -19219)return "C";
    if ($asc >= -19218 and $asc <= -18711)return "D";
    if ($asc >= -18710 and $asc <= -18527)return "E";
    if ($asc >= -18526 and $asc <= -18240)return "F";
    if ($asc >= -18239 and $asc <= -17923)return "G";
    if ($asc >= -17922 and $asc <= -17418)return "H";
    if ($asc >= -17417 and $asc <= -16475)return "J";
    if ($asc >= -16474 and $asc <= -16213)return "K";
    if ($asc >= -16212 and $asc <= -15641)return "L";
    if ($asc >= -15640 and $asc <= -15166)return "M";
    if ($asc >= -15165 and $asc <= -14923)return "N";
    if ($asc >= -14922 and $asc <= -14915)return "O";
    if ($asc >= -14914 and $asc <= -14631)return "P";
    if ($asc >= -14630 and $asc <= -14150)return "Q";
    if ($asc >= -14149 and $asc <= -14091)return "R";
    if ($asc >= -14090 and $asc <= -13319)return "S";
    if ($asc >= -13318 and $asc <= -12839)return "T";
    if ($asc >= -12838 and $asc <= -12557)return "W";
    if ($asc >= -12556 and $asc <= -11848)return "X";
    if ($asc >= -11847 and $asc <= -11056)return "Y";
    if ($asc >= -11055 and $asc <= -10247)return "Z";
    return null;
}

function mb_str_split2($string) {
    return preg_split('/(?<!^)(?!$)/u', $string);
}

function get_letter($string) {
    $charlist = mb_str_split2($string);
    //只返回首个字符首字母大写
    return getfirstchar($charlist['0']);
}

function upload_img($tmp){
    new Storage();
    //if ($request->isMethod('POST')) { //判断文件是否是 POST的方式上传
        $path = '/images'; //public下的article
        if ($tmp->isValid()) { //判断文件上传是否有效
            $FileType = $tmp->getClientOriginalExtension(); //获取文件后缀

            $FilePath = $tmp->getRealPath(); //获取文件临时存放位置

            $FileName = date('Y-m-d') . uniqid() . '.' . $FileType; //定义文件名

            Storage::disk('img')->put('/images/'.date('Y-m-d').'/'.$FileName,file_get_contents($FilePath)); //存储文件

            return $path . '/'.date('Y-m-d').'/' . $FileName;//文件路径

        }
    //}
}

/**
 * 获取数字验证码
 * @param int $length 验证码长度
 */
function getNumCode($length){
    $max = pow(10, $length) - 1;
    $min = pow(10, $length - 1);
    return rand($min, $max);
}